# -*- coding: utf-8 -*-
import sys

sys.path.append("../../")
from zinx.znet.server import NewServer
from zinx.znet.router import NewServer
from zinx.ziface.irequest import IRequest


class PingRouter(BaseRouter):
    """
    用户自定义路由
    """

    def __init__(self):
        super().__init__()

    def Handle(self, request: IRequest):
        """
        处理conn业务的方法
        :param request:
        :return:
        """
        try:
            print("调用Handle")
            # request.GetConnection().GetTCPConnection().send("ping\n".encode("GB2312"))
            print(request.GetData())
            request.GetConnection().SendMsg(1, "ping\n".encode("GB2312"))
            request.GetConnection().SendMsg(1, request.GetData())
        except Exception as e:
            print("ping异常", e)


def ConnectionStart(con):
    print("======Start========", con.GetConnID())


def ConnectionStop(con):
    print("======Stop========", con.GetConnID())


if __name__ == '__main__':
    server = NewServer()
    server.AddRouter(1, PingRouter())
    server.SetOnConnStart(ConnectionStart)
    server.SetOnConnStop(ConnectionStop)
    server.Serve()
